package com.ktg.mes.erpUnitMeasure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.mes.erpUnitMeasure.entity.T6ErpUnitMeasure;
import com.ktg.mes.erpUnitMeasure.service.IErpUnitMeasureService;
import com.ktg.mes.erpUnitMeasure.service.IT6ErpUnitMeasureService;
import com.ktg.mes.md.domain.MdUnitMeasure;
import com.ktg.mes.md.service.IMdUnitMeasureService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ErpUnitMeasureServiceImpl implements IErpUnitMeasureService {
    private static final Logger log = LoggerFactory.getLogger(ErpUnitMeasureServiceImpl.class);

    @Autowired
    private IT6ErpUnitMeasureService it6ErpUnitMeasureService;

    @Autowired
    private IMdUnitMeasureService iMdUnitMeasureService;

    @Override
    public void syncDataListFromErp() {
        Long maxPubufts = 0L;
        Long dbMaxPubufts = iMdUnitMeasureService.queryMaxPubufts();
        if (dbMaxPubufts != null) {
            maxPubufts = dbMaxPubufts;
        }
        List<T6ErpUnitMeasure> list = it6ErpUnitMeasureService.queryListByLastPubufts(maxPubufts);
        for (T6ErpUnitMeasure srcItem : list) {
            MdUnitMeasure distItem = it6ErpUnitMeasureService.toMdUnitMeasure(srcItem);
            // 先查询是否存在此单位信息
            MdUnitMeasure dbItem = iMdUnitMeasureService.selectMdUnitMeasureByMeasureCode(distItem.getMeasureCode());
            if (dbItem == null) {
                if (srcItem.getBMainUnit() == 0) {
                    //不存在父单位信息，先插入父单位信息
                    QueryWrapper<T6ErpUnitMeasure>wrapper = new QueryWrapper<>();
                    wrapper.select("cComunitCode", "cComUnitName", "cGroupCode", "cBarCode", "bMainUnit", "iChangRate","CONVERT(BIGINT,pubufts) as pubufts").eq("cGroupCode",srcItem.getCGroupCode()).eq("bMainUnit",1);
                    T6ErpUnitMeasure parentT6ErpUnitMeasure = it6ErpUnitMeasureService.getOne(wrapper);
                    if (parentT6ErpUnitMeasure != null) {
                        MdUnitMeasure parentUnitMeasure = iMdUnitMeasureService.selectMdUnitMeasureByMeasureCode(parentT6ErpUnitMeasure.getCComunitCode());
                        if (parentUnitMeasure == null) {
                            MdUnitMeasure pItem = it6ErpUnitMeasureService.toMdUnitMeasure(parentT6ErpUnitMeasure);
                            int pid = iMdUnitMeasureService.insertMdUnitMeasure(pItem);
                            distItem.setPrimaryId(pItem.getMeasureId());
                        }else {
                            distItem.setPrimaryId(parentUnitMeasure.getMeasureId());
                        }

                    }
                }

                iMdUnitMeasureService.insertMdUnitMeasure(distItem);

            }else {
                iMdUnitMeasureService.updateMdUnitMeasure(distItem);
            }
        }


    }

    public static void main(String[] args) {
        String pid = "010101";
        System.out.println(pid.substring(0,pid.length() - 2 ));
    }
}
